import { setToken, clearToken, http } from '@/utils'
// 登录action
export const login = (mobile, code) => {
  return async dispatch => {
    // 登录请求
    const data = await http.post('/authorizations', { mobile, code })
    // 本地存储token
    setToken(data.token)
    // rudex存储token
    dispatch({ type: 'user/setToken', payload: data.token })
  }
}

// 获取用户信息
export const getUserInfo = () => {
  return async (dispatch) => {
    // 用户信息请求
    const data = await http.get('/user/profile')
    // data.name 数据
    dispatch({ type: 'user/setName', payload: data.name })
  }
}

// 退出登录
export const logout = () => {
  return dispatch => {
    // 本地token清空
    clearToken()
    // 用户信息清空
    dispatch({ type: 'user/setToken', payload: '' })
    dispatch({ type: 'user/setName', payload: '' })
  }
}